package when3meet.view;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.SpringLayout;

import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JButton;

import when3meet.controller.AddChoiceController;
import when3meet.controller.AddEdgeController;
import when3meet.model.ClientInfo;
import when3meet.utilities.SpringUtilities;

import java.awt.Font;
import javax.swing.SwingConstants;

/**
 * TODO fix the frame to full screen
 * 
 * @author Xi
 * 
 */
public class MainFrame extends JFrame {

	JPanel contentPane;
	static MainFrame frame;
	DecisionLinePanel decisionLineZone;
	public JTextField choiceField;
	public JButton addEdgeBtn;
	public JButton addChoiceBtn;
	JLabel eventNameLabel;
	JPanel displayLabel;
	private JLabel addEdgeErrorLabel;
	JPanel userPanel;
	KickUserButton[] kickButtons;
	JLabel errorLabel;
	JTextField eventIdLabel;

	/**
	 * Create the main frame
	 */
	private MainFrame(int numEdges, String[] choices) {

		/* Basic setting */
		getContentPane().setBackground(Color.LIGHT_GRAY);
		getContentPane().setForeground(Color.WHITE);
		setTitle("Main Application");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(1080, 720);
		setResizable(false);
		setLocationRelativeTo(null);

		decisionLineZone = DecisionLinePanel.getInstance(numEdges, choices);
		// decisionLineZone = new JPanel();
		decisionLineZone.setBackground(Color.GRAY);

		displayLabel = new JPanel();
		displayLabel.setBackground(Color.GRAY);

		choiceField = new JTextField();
		choiceField.setColumns(10);

		JLabel lblAddChoice = new JLabel("Add choice:");
		lblAddChoice.setFont(new Font("Tahoma", Font.BOLD, 14));

		addChoiceBtn = new JButton("Submit Choice");
		addChoiceBtn.addActionListener(new AddChoiceController(this));

		addEdgeBtn = new JButton("Add Edge");
		addEdgeBtn.addActionListener(new AddEdgeController(this));

		eventNameLabel = new JLabel("Event Name:");
		eventNameLabel.setFont(new Font("Monospaced", Font.BOLD, 21));
		eventNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JLabel lblCurrentUsers = new JLabel("Current Users:");
		lblCurrentUsers.setFont(new Font("Tahoma", Font.BOLD, 14));

		userPanel = new JPanel(new SpringLayout());
		userPanel.setBackground(Color.GRAY);

		kickButtons = new KickUserButton[11];
		for (int i = 0; i < 11; i++) {
			kickButtons[i] = new KickUserButton(" ");
			kickButtons[i].setVisible(false);
			userPanel.add(kickButtons[i]);
		}
		SpringUtilities.makeCompactGrid(userPanel, 11, 1, 6, 6, 6, 6);
		ClientInfo.getInstance().setKickBtn(kickButtons);

		errorLabel = new JLabel("Trying to crash me huh?");
		errorLabel.setVisible(false);
		errorLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
		errorLabel.setForeground(Color.RED);
		errorLabel.setHorizontalAlignment(SwingConstants.CENTER);

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addGap(43)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.LEADING)
										.addComponent(errorLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 848,
												Short.MAX_VALUE)
										.addComponent(displayLabel, GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE)
										.addComponent(decisionLineZone, GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE)
										.addComponent(eventNameLabel, GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE))
						.addGap(18)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.LEADING)
										.addComponent(userPanel, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
										.addComponent(addChoiceBtn, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
										.addComponent(addEdgeBtn, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
										.addComponent(lblAddChoice, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 155,
												Short.MAX_VALUE)
										.addComponent(choiceField, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
										.addComponent(lblCurrentUsers, GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
						.addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.TRAILING)
										.addComponent(eventNameLabel, GroupLayout.PREFERRED_SIZE, 47,
												GroupLayout.PREFERRED_SIZE).addComponent(lblAddChoice))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.LEADING, false)
										.addGroup(
												groupLayout
														.createSequentialGroup()
														.addGap(7)
														.addComponent(choiceField, GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.UNRELATED)
														.addComponent(addChoiceBtn, GroupLayout.PREFERRED_SIZE, 31,
																GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED,
																GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(lblCurrentUsers)
														.addGap(18)
														.addComponent(userPanel, GroupLayout.PREFERRED_SIZE, 400,
																GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED))
										.addComponent(decisionLineZone, GroupLayout.PREFERRED_SIZE, 529,
												GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.LEADING, false)
										.addComponent(addEdgeBtn, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addComponent(displayLabel, GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
						.addPreferredGap(ComponentPlacement.RELATED).addComponent(errorLabel).addGap(15)));

		addEdgeErrorLabel = new JLabel("Please do not excede the height of choice!");
		addEdgeErrorLabel.setVisible(false);
		addEdgeErrorLabel.setHorizontalAlignment(SwingConstants.CENTER);
		addEdgeErrorLabel.setForeground(new Color(255, 0, 0));
		addEdgeErrorLabel.setFont(new Font("Tahoma", Font.BOLD, 14));

		eventIdLabel = new JTextField("");
		eventIdLabel.setBackground(Color.GRAY);
		eventIdLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
		eventIdLabel.setHorizontalAlignment(SwingConstants.CENTER);
		eventIdLabel.setEditable(false);
		GroupLayout gl_displayLabel = new GroupLayout(displayLabel);
		gl_displayLabel.setHorizontalGroup(gl_displayLabel.createParallelGroup(Alignment.LEADING)
				.addComponent(addEdgeErrorLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE)
				.addComponent(eventIdLabel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 848, Short.MAX_VALUE));
		gl_displayLabel.setVerticalGroup(gl_displayLabel.createParallelGroup(Alignment.LEADING).addGroup(
				gl_displayLabel.createSequentialGroup().addComponent(addEdgeErrorLabel)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(eventIdLabel, GroupLayout.DEFAULT_SIZE, 17, Short.MAX_VALUE)));
		displayLabel.setLayout(gl_displayLabel);
		getContentPane().setLayout(groupLayout);

		/* Load decision information */

	}

	public static void clearInstance() {
		frame = null;
	}

	public JTextField getChoiceField() {
		return choiceField;
	}

	public static MainFrame getInstance() {
		if (frame == null)
			frame = new MainFrame(0, null);

		return frame;
	}

	public DecisionLinePanel getDecisionLineZone() {
		return decisionLineZone;
	}

	public JButton getAddEdgeButton() {
		return addEdgeBtn;
	}

	public JButton getAddChoiceButton() {
		return addChoiceBtn;
	}

	public void setEventName(String question) {
		eventNameLabel.setText(question);
	}

	public JLabel getAddEdgeError() {
		return addEdgeErrorLabel;
	}

	public JPanel getUserPanel() {
		return userPanel;
	}

	public JLabel getErrorLabel() {
		return errorLabel;
	}

	public JTextField getEventIdLabel() {
		return eventIdLabel;
	}

	public void setDecisionLinePanel(DecisionLinePanel p) {
		decisionLineZone = p;

	}
}